import * as Cesium from 'cesium';

/**
 * Entity utility functions
 */

/**
 * Create a polygon
 * @param {Cesium.Viewer} viewer - The Cesium viewer instance
 * @param {Object} options - Polygon options
 * @param {Array} options.positions - Array of longitude/latitude pairs
 * @param {Cesium.Color} options.color - Base color
 * @param {Number} options.extrudedHeight - Extrusion height
 * @param {String} options.name - Polygon name
 */
export function createPolygon(viewer, options) {
  const {
    positions,
    color = Cesium.Color.WHITE,
    extrudedHeight = 0,
    name = 'Polygon'
  } = options;
  
  return viewer.entities.add({
    name,
    polygon: {
      hierarchy: Cesium.Cartesian3.fromDegreesArray(positions),
      material: color,
      outline: true,
      outlineColor: Cesium.Color.WHITE,
      height: 0,
      extrudedHeight
    }
  });
}

/**
 * Create a point
 * @param {Cesium.Viewer} viewer - The Cesium viewer instance
 * @param {Object} options - Point options
 * @param {Number} options.lon - Longitude
 * @param {Number} options.lat - Latitude
 * @param {Cesium.Color} options.color - Point color
 * @param {Number} options.size - Point size in pixels
 * @param {String} options.name - Point name
 */
export function createPoint(viewer, options) {
  const {
    lon,
    lat,
    color = Cesium.Color.WHITE,
    size = 10,
    name = 'Point'
  } = options;
  
  return viewer.entities.add({
    name,
    position: Cesium.Cartesian3.fromDegrees(lon, lat),
    point: {
      pixelSize: size,
      color,
      outlineColor: Cesium.Color.WHITE,
      outlineWidth: 2
    }
  });
}

/**
 * Create a billboard
 * @param {Cesium.Viewer} viewer - The Cesium viewer instance
 * @param {Object} options - Billboard options
 * @param {Number} options.lon - Longitude
 * @param {Number} options.lat - Latitude
 * @param {String} options.image - Image URL
 * @param {Number} options.scale - Billboard scale
 * @param {Cesium.Color} options.color - Billboard color
 * @param {String} options.name - Billboard name
 */
export function createBillboard(viewer, options) {
  const {
    lon,
    lat,
    image,
    scale = 1.0,
    color = Cesium.Color.WHITE,
    name = 'Billboard'
  } = options;
  
  return viewer.entities.add({
    name,
    position: Cesium.Cartesian3.fromDegrees(lon, lat),
    billboard: {
      image,
      scale,
      color
    }
  });
}

/**
 * Create a label
 * @param {Cesium.Viewer} viewer - The Cesium viewer instance
 * @param {Object} options - Label options
 * @param {Number} options.lon - Longitude
 * @param {Number} options.lat - Latitude
 * @param {String} options.text - Label text
 * @param {Cesium.Color} options.color - Label color
 * @param {String} options.font - Label font
 * @param {String} options.name - Label name
 */
export function createLabel(viewer, options) {
  const {
    lon,
    lat,
    text,
    color = Cesium.Color.WHITE,
    font = '14px sans-serif',
    name = 'Label'
  } = options;
  
  return viewer.entities.add({
    name,
    position: Cesium.Cartesian3.fromDegrees(lon, lat),
    label: {
      text,
      font,
      fillColor: color,
      outlineColor: Cesium.Color.BLACK,
      outlineWidth: 2,
      style: Cesium.LabelStyle.FILL_AND_OUTLINE,
      verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
      pixelOffset: new Cesium.Cartesian2(0, -15)
    }
  });
}

export default {
  createPolygon,
  createPoint,
  createBillboard,
  createLabel
};